package com.beiming.libra.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.beiming.libra.common.page.Page;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.dao.FileInfoMapper;
import com.beiming.libra.dao.PapersTemplateMapper;
import com.beiming.libra.domain.FileInfo;
import com.beiming.libra.domain.PapersTemplate;
import com.beiming.libra.domain.dto.requestdto.PapersTemplateRequestDTO;
import com.beiming.libra.domain.dto.responsedto.PapersTemplateResponseDTO;
import com.beiming.libra.service.PapersTemplateService;

@Service
public class PapersTemplateServiceImpl implements PapersTemplateService {

  @Resource
  private PapersTemplateMapper papersTemplateMapper;

  @Resource
  private FileInfoMapper fileInfoMapper;

  @Override
  public PageResult<PapersTemplateResponseDTO> listPapersTemplate(PapersTemplateRequestDTO dto) {

    List<PapersTemplateResponseDTO> resDtoList = new ArrayList<PapersTemplateResponseDTO>();

    PapersTemplateResponseDTO resDto = new PapersTemplateResponseDTO();
    List<PapersTemplate> list = papersTemplateMapper.listPapers(dto.getShowName(),
        (dto.getPageNo() - 1) * dto.getPageSize(), dto.getPageSize());
    for (PapersTemplate temp : list) {

      String fileId = temp.getFileId();
      FileInfo fileinfo = fileInfoMapper.selectByPrimaryKey(fileId);
      resDto.setPapers(temp);
      resDto.setPapersInfo(fileinfo);
      resDtoList.add(resDto);

    }
    PageResult<PapersTemplateResponseDTO> pageResult = new PageResult<PapersTemplateResponseDTO>();
    pageResult.setList(resDtoList);
    int total = papersTemplateMapper.listPapersCount(dto.getShowName());
    Page page = new Page();

    page.setPageNo(dto.getPageNo());
    page.setPageSize(dto.getPageSize());
    page.setTotalRecords(total);
    pageResult.setPage(page);
    return pageResult;
  }

}
